import React, { Component } from 'react'
import {Vote,SelectInter} from '../types/vote'
import SelectItem from './select_item/select_item'
//moment @types/moment
import moment from 'moment'

interface IProps{
    item:Vote
}

export default class VoteItem extends Component<IProps>{
    render() {
        let {title,con,voteUsers,items,createUser,endTime,vid} = this.props.item;
        let curUser:any = localStorage.getItem('username');
        //是否投过票的状态
        let status:boolean = voteUsers.includes(curUser); //boolean
        //投票的总数
        let total = items.reduce((pre:number,cur:SelectInter) => {
            return pre+cur.num
        },0)
        //计算距离结束还有多少天
        let days = moment(endTime).diff(moment(new Date()),'days');
        return (
            <div>
                <h4>{title}</h4>
                <div>
                    <span>{con}</span>
                    <div className="select-item">
                        {items.length && items.map((item:SelectInter) => <SelectItem key={item.id} total={total} item={item} status={status} vid={vid}></SelectItem>)}
                    </div>
                    <div>
                        <span>{voteUsers.length}人参加</span>---
                        <span>天数:{days}</span>----
                        <span>由{createUser}创建</span>
                    </div>
                </div>
            </div>
        )
    }
}
